package com.loan.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.loan.entity.Message;
import com.loan.service.MessageService;
import com.loan.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * @author z
 */
@Api(tags = "贷款资讯管理")
@RestController
@RequestMapping("/message")
public class MessageController {
    @Resource
    private MessageService messageService;

    @ApiOperation(value = "首页资讯展示",httpMethod = "GET",notes = "展示5条最新资讯")
    @GetMapping
    public ResponseResult<Object> getMessage(){
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Message::getCreateTime);
        queryWrapper.last("limit 5"); // 添加限制结果数的方法
        List<Message> list = messageService.list(queryWrapper);
        return ResponseResult.success(list);
    }

    @ApiOperation(value = "资讯详情",httpMethod = "POST",notes = "展示某条资讯的详情信息")
    @PostMapping("/{mid}")
    public ResponseResult<Message> getMessageById(@PathVariable Integer mid){
        Message messageById = messageService.getMessageById(mid);
        return ResponseResult.success(messageById);
    }

    @ApiOperation(value = "所有资讯展示",httpMethod = "GET",notes = "展示所有资讯")
    @GetMapping("/all")
    public ResponseResult<List<Message>> getAllMessage(){
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Message::getCreateTime);
        List<Message> list = messageService.list(queryWrapper);
        return ResponseResult.success(list);
    }
}
